package com.hq.api;


import com.hq.pojo.Admin;

import com.hq.service.AdminService;

import com.hq.utils.MassageJson_no;
import com.hq.utils.MassageJson;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminApi {
    @Resource
    AdminService adminService;


    @RequestMapping(value = "/getadmin",method = RequestMethod.POST)
    public MassageJson getAdmin(String admin_id){
        Admin admin = null;
        try{
            admin = adminService.getAdmin(admin_id);
            System.out.println("管理员的id是"+admin_id);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(admin!=null){
            return  new MassageJson(200,"请求成功",admin);
        }else {
            return  new MassageJson(201,"请求失败",admin);
        }
    }

    //管理员登录
    @RequestMapping("/logadmin")
    public MassageJson logAdmin(@RequestBody HashMap<String, Object> map){
        Admin admin = null;
        System.out.println(map.size()+"+++++++++++++++++++++++");
        try{
            admin = adminService.logAdmin(map.get("admin_name")+"",map.get("admin_password")+"");
        }catch (Exception e){
            e.printStackTrace();
        }
        if(admin!=null){
            return  new MassageJson(200,"登录成功",admin);
        }else {
            return  new MassageJson(201,"登录失败",admin);
        }
    }

    @RequestMapping(value = "/addAdmin",method = RequestMethod.POST)
    public MassageJson addAdmin(Admin admin){

        int i = 0;
        try{
            i = adminService.addAdmin(admin);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(i>0){
            return new MassageJson(200,"添加成功",admin);
        }else {
            return new MassageJson(201,"添加失败",admin);
        }

    }

    @RequestMapping(value = "/delAdmin",method = RequestMethod.POST)
    public MassageJson delAdmin(String admin_id){
        int i = 0;
        try{
            i = adminService.delAdmin(admin_id);
            System.out.println("要删除的管理员id是"+admin_id);
        }catch (Exception e){
            e.printStackTrace();
        }
        if(i>0){
            return  new MassageJson(200,"删除成功",i);
        }else {
            return  new MassageJson(201,"删除失败",i);
        }
    }

    @RequestMapping(value = "/selAdmin",method = RequestMethod.POST)
    public MassageJson selAdmin(){
        List<Admin> adminList = null;
        try {
            adminList=adminService.selAdmin();
            if(adminList!=null){
                return new MassageJson(200,"请求成功",adminList);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new MassageJson(201,"登录失败",adminList);
    }





}
